﻿using System;
using System.ComponentModel;
using System.Linq;

namespace Taschenrechner {
    public class Controler : INotifyPropertyChanged {

        ProcessManager processManager;
        char charToProcess;

        internal Controler() {
            processManager = new ProcessManager();
            processManager.ProcessManagerPropertyChanged += new PropertyChangedEventHandler(OnBufferChange);
            processManager.ProcessManagerPropertyChanged += new PropertyChangedEventHandler(OnResultChange);
        }

        public string Display {
            get { return processManager.NumberString; }
        }

        public string Result {
            get { return processManager.ResultString; }
        }

        public void ClearEntry() {
            processManager.ClearEntry();
        }

        public void Clear() {
            processManager.Clear();
        }

        public bool ProcessChar(char charToProcess) {
            this.charToProcess = charToProcess;
            bool hasProcessChar = true;

            try {
                if (IsDigitChar()) {
                    hasProcessChar = processManager.ProcessDigitChar(charToProcess);
                }
                else if (IsOperatorChar()) {
                    hasProcessChar = processManager.ProcessOperatorChar(charToProcess);
                }
                else if (IsResultChar()) {
                    hasProcessChar = processManager.ProcessResultChar();
                }
                else {
                    hasProcessChar = false;
                }
            }
            catch (OverflowException e) {
                Clear();
                throw e;
            }
            catch (DivideByZeroException e) {
                Clear();
                throw e;
            }
            catch (Exception e) {
                throw e;
            }

            return hasProcessChar;
        }

        private bool IsDigitChar() {
            if (ValidChar.digits.Contains(charToProcess)) {
                return true;
            }
            return false;
        }

        private bool IsOperatorChar() {
            if (ValidChar.operators.Contains(charToProcess)) {
                return true;
            }
            return false;
        }

        private bool IsResultChar() {
            if (ValidChar.results.Contains(charToProcess)) {
                return true;
            }
            return false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        internal void OnBufferChange(object sourece, PropertyChangedEventArgs e) {
            NotifyPropertyChanged("Display");
        }

        internal void OnResultChange(object sourece, PropertyChangedEventArgs e) {
            NotifyPropertyChanged("Result");
        }


    }
}
